<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    姓名:<input type="text" class="name" /><br />
    图片:<input type="file" class="img" /><br />
    <button>点击提交文件</button>
    <script>
      let nameEle = document.querySelector(".name");
      let imgEle = document.querySelector(".img");
      let btn = document.querySelector("button");
      btn.onclick = function () {
        let name = nameEle.value;
        let img = imgEle.files[0];
        let form = new FormData();
        form.append("name", name);
        form.append("img", img);
        console.log(name, img);
        let xhr = new XMLHttpRequest();
        xhr.open("POST", "file.php", true);
        xhr.onload = function () {
          console.log(xhr.responseText);
        };
        xhr.send(form);
      };
    </script>
  </body>
</html>
